package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Topic;
import com.daka.enumerate.DictionaryMap;
import com.daka.enumerate.StatusCode;
import com.daka.enumerate.TopicEnum;
import com.daka.service.IndexDataService;
import com.daka.service.TopicService;
import com.daka.tools.CheckLogin;
import com.daka.vo.db.TopicCommentVolunteerVo;
import com.daka.vo.request.TopicListRequestVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/topic")
@Slf4j
public class TopicController {

    @Resource(name = "topicService")
    private TopicService topicService;

    @Resource
    private IndexDataService indexDataService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Topic> list(@Valid @RequestBody TopicListRequestVo topicListRequestVo, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new ListDataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }


        List<Topic> topicList = topicService.selectByParam(topicListRequestVo);
        return new ListDataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> save(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        Topic result = topicService.save(topic);

        if (result == null) {
            return new DataResponseVo<Topic>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/info", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> info(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        Topic result = topicService.selectById(topic.getId());

        if (result == null) {
            return new DataResponseVo<Topic>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/effective", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> effective(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        topic.setDisplay(TopicEnum.DISTORY_YES.getStatusCode());
        Topic result = topicService.updateDisplay(topic);
        if (result == null) {
            return new DataResponseVo<Topic>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        indexDataService.updateDelStatus(0,result.getId(),null,null);

        return new DataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/invalid", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> invalid(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        topic.setDisplay(TopicEnum.DISTORY_NO.getStatusCode());
        Topic result = topicService.updateDisplay(topic);
        if (result == null) {
            return new DataResponseVo<Topic>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        indexDataService.updateDelStatus(1,result.getId(),null,null);

        return new DataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateTop", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> updateTop(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Topic>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        topic.setDisplay(TopicEnum.DISTORY_NO.getStatusCode());
        Topic result = topicService.updateTop(topic);
        if (result == null) {
            return new DataResponseVo<Topic>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<Topic>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/comment", consumes = "application/json")
    @Login
    public ListDataResponseVo<TopicCommentVolunteerVo> comment(@Valid @RequestBody Topic topic, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new ListDataResponseVo<TopicCommentVolunteerVo>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        List<TopicCommentVolunteerVo> result = topicService.selectCommentById(topic.getId());
        if (result == null) {
            return new ListDataResponseVo<TopicCommentVolunteerVo>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new ListDataResponseVo<TopicCommentVolunteerVo>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
